﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

namespace SmartProject.Entity
{
    public class TaskCategory : EntityBase
    {
        public virtual string Description { get; set; }

        public static TaskCategory Add(ISession session, string description)
        {
            TaskCategory taskCategory = GetTaskCategory(session, description);
            if (taskCategory == null)
            {
                taskCategory = new TaskCategory();
            }
            taskCategory.Description = description;
            taskCategory.Save(session);
            return taskCategory;
        }

        public static IList<TaskCategory> GetTaskCategories(ISession session)
        {
            IList<EntityBase> entities = Find(session, null, null, typeof(TaskCategory));
            return (from p in entities select p as TaskCategory).ToList();
        }

        public static TaskCategory GetTaskCategory(ISession session, int taskCategoryId)
        {
            IList<ICriterion> expressions = new List<ICriterion>();
            expressions.Add(Restrictions.Eq("Id", taskCategoryId));
            IList<EntityBase> entities = Find(session, expressions, null, typeof(TaskCategory));
            if (entities != null && entities.Count == 1)
            {
                return (TaskCategory)entities[0];
            }
            return null;
        }

        public static TaskCategory GetTaskCategory(ISession session, string description)
        {
            IList<ICriterion> expressions = new List<ICriterion>();
            expressions.Add(Restrictions.Eq("Description", description));
            IList<EntityBase> entities = Find(session, expressions, null, typeof(TaskCategory));
            if (entities != null && entities.Count == 1)
            {
                return (TaskCategory)entities[0];
            }
            return null;
        }
    }
}
